package com.hexiang.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hexiang.domain.User;
import com.hexiang.service.UserService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@RestController
@Api(tags = "用户数据接口")
@Slf4j
public class UserController {

    @Autowired
    @Qualifier("userServiceImpl")
    private UserService userService;

    @ApiOperation(value = "查询所有用户", notes = "查询所有用户")
    @GetMapping("/users")
    public Object getUsers() {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return userService.findAll();
    }

    @ApiOperation(value = "查询用户", notes = "根据 name 查询用户")
    @ApiImplicitParam(paramType = "path", name = "name", value = "用户名", required = true)
    @GetMapping("/user/{name}")
    public User getUserByName(@PathVariable String name) {
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return userService.findByName(name);
    }

    @ApiOperation(value = "新增用户", notes = "根据传入的用户名和年龄添加一个新用户")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "name", value = "用户名", required = true, defaultValue = "test"),
            @ApiImplicitParam(paramType = "query", name = "age", value = "年龄", required = true, defaultValue = "123")})
    @PostMapping("/user")
    public Object addUser(@RequestHeader Map<String, String> header, @RequestParam String name, @RequestParam Integer age) {
        return userService.save(name, age);
    }

    @ApiOperation(value = "新增用户", notes = "根据传入的用户名和年龄添加一个新用户，返回用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "name", value = "用户名", required = true, defaultValue = "test"),
            @ApiImplicitParam(paramType = "query", name = "age", value = "年龄", required = true, defaultValue = "123")})
    @PostMapping("/userReturn")
    public Object addUser(User user) {
        return userService.saveByUser(user);
    }

    @ApiOperation(value = "删除用户", notes = "根据 id 删除用户")
    @ApiResponses({@ApiResponse(code = 200, message = "删除成功！"), @ApiResponse(code = 500, message = "删除失败！")})
    @DeleteMapping("/user/{id}")
    public Integer deleteUserById(@PathVariable Long id) {
        return userService.delete(id);
    }

    @ApiOperation(value = "修改用户", notes = "传入用户信息进行更新修改")
    @PutMapping("/user")
    public Object updateUser(@Validated @RequestBody User user) {
        return userService.update(user);
    }

    @ApiOperation(value = "查询所有用户(分页)", notes = "查询所有用户(分页)")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页码", required = true, defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页条数", required = true, defaultValue = "2")})
    @GetMapping("/user/page")
    public Object getUserPage(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "2") Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(userService.findAll());
    }

    @GetMapping("/user/err")
    public Object errTest() {
        Random random = new Random();
        int num = random.nextInt(10);
        log.info("num is {}", num);
        if (num % 2 == 0) {
            throw new RuntimeException();
        }
        return num;
    }

}